package com.datang.pet.service.impl;/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-07-18 22:46
 * @notify
 * @version 1.0
 */

import com.datang.pet.common.utils.StringUtils;
import com.datang.pet.mapper.SuggestMapper;
import com.datang.pet.mapperExtend.ResourceMapperExtend;
import com.datang.pet.pojo.Resource;
import com.datang.pet.pojo.Suggest;
import com.datang.pet.service.SuggestService;
import com.datang.pet.vo.AddSuggestRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class SuggestServiceImpl implements SuggestService {

    @Autowired
    private SuggestMapper suggestMapper;

    @Autowired
    private ResourceMapperExtend resourceMapperExtend;

    public void addSuggest(AddSuggestRequest suggestRequest) throws Exception {
        String urlId = null;
        if (!suggestRequest.getResources().isEmpty()) {
            try {
                 urlId = StringUtils.getId();
                for(Resource r :suggestRequest.getResources()){
                    r.setId(urlId);
                }
                resourceMapperExtend.addResourceList(suggestRequest.getResources());
            } catch (Exception e) {
                throw new Exception("添加图片失败");
            }
        }


        suggestRequest.setId(StringUtils.getId());
        suggestRequest.setCreateDate(new Date());
        Suggest suggest = new Suggest();
        BeanUtils.copyProperties(suggestRequest, suggest);
        suggest.setResourceId(urlId);

        try {
            suggestMapper.insert(suggest);
        } catch (Exception e) {
            throw new Exception("添加反馈失败");
        }

    }
}
